package org.rslai.autotest.core.page.impl;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.rslai.autotest.core.page.AbstractHelper;
import org.rslai.autotest.core.page.Script;
import org.springframework.beans.factory.annotation.Value;

/**
 * Script处理方法
 * @author rslai
 */
public class ScriptImpl extends AbstractHelper implements Script{
	@Value("${webdriverwait.timeout}")
	private int webdriverwaitTimeout; // 使用wait查找对象的超时时间

	@Override
	public void setJsMethodContent(String methodName, String methodContent) {
		this.executeScript(methodName + "=" + methodContent.replace(methodName, ""));
	}

	@Override
	public String getJsMethodContent(String methodName) {
		return (String)this.executeScript("return " + methodName + ".toString();");
	}

	@Override
	public void highlight(By by) {
		WebElement element = this.driver().findElement(by);
		this.executeScript("arguments[0].style.border = \"5px solid yellow\"", element);
	}

	@Override
	public void clickLink(String text) {
		this.clickLink(text, 0);
	}

	@Override
	public void clickLink(String text, int index) {
		this.executeScript("$(\"a:contains('" + text + "')\").get("+ (index - 1) + ").click();");
	}

	@Override
	public Object executeScript(String script, Object... args) {
		return ((JavascriptExecutor) this.driver()).executeScript(script, args);
	}

	@Override
	public Object executeAsyncScript(String script, Object... args) {
		return ((JavascriptExecutor) this.driver()).executeAsyncScript(script, args);
	}

	@Override
	public void waitForCondition(String script, Object... args) {
		this.waitForCondition(script, 0, args);
	}

	@Override
	public void waitForCondition(String script, int timeout, Object... args) {
		if (timeout == 0) {
			timeout = webdriverwaitTimeout;
		}

		for (int i=0; i<timeout; i++) {	
			Boolean result = (Boolean) executeScript(script, args);
			if (result!=null && result) {
				return;
			}
			super.sleep(1);
		}

		throw new TimeoutException("超时错误：经过" + timeout + "秒后js (" + script + ") 执行结果仍没返回true。");
	}

	@Override
	public void waitForJQueryCompleted(Object... args) {
		waitForJQueryCompleted(webdriverwaitTimeout, args);
	}

	@Override
	public void waitForJQueryCompleted(int timeout, Object... args) {
		this.waitForCondition("return jQuery.active == 0;", timeout, args);
	}

	@Override
	public void waitForPrototypeCompleted(Object... args) {
		waitForPrototypeCompleted(webdriverwaitTimeout, args);
	}

	@Override
	public void waitForPrototypeCompleted(int timeout, Object... args) {
		this.waitForCondition("return Ajax.activeRequestCount == 0;", timeout, args);
	}

	@Override
	public void waitForDojoCompleted(Object... args) {
		waitForDojoCompleted(webdriverwaitTimeout, args);
	}

	@Override
	public void waitForDojoCompleted(int timeout, Object... args) {
		this.waitForCondition("return dojo.io.XMLHTTPTransport.inFlight.length == 0;", timeout, args);
	}

}
